package com.lj.luoye.lotteryticket.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lj.luoye.lotteryticket.R;
import com.tuo.customview.VerificationCodeView;


/**
 * 公告弹窗
 */

public class PasswrdInputDialog extends Dialog {

    public PasswrdInputDialog(@NonNull Context context) {
        super(context);
    }

    public PasswrdInputDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity= Gravity.CENTER;
        layoutParams.width= WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height= WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setPadding(20, 0, 20, 0);
        getWindow().setAttributes(layoutParams);

    }

    public static class Builder {

        private String title;
        private String message;
        private String btn1Text;
        private String btn2Text;
        private PasswordDialogListener passwordDialogListener;


        private View layout;
        private PasswrdInputDialog dialog;
        public Builder(Context context) {
            //这里传入自定义的style，直接影响此Dialog的显示效果。style具体实现见style.xml
            dialog = new PasswrdInputDialog(context, R.style.MyDialog);
            dialog.setCancelable(false);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.pwd_input_dialog_layout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        public Builder(Context context, int resource) {
            //这里传入自定义的style，直接影响此Dialog的显示效果。style具体实现见style.xml
            dialog = new PasswrdInputDialog(context, R.style.MyDialog);
            dialog.setCancelable(false);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(resource, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        public String getTitle() {
            return title;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setBtn1Text(String btn1Text) {
            this.btn1Text = btn1Text;
            return this;
        }

        public Builder setBtn2Text(String btn2Text) {
            this.btn2Text = btn2Text;
            return this;
        }

        public Builder setPasswordDialogListener(PasswordDialogListener passwordDialogListener) {
            this.passwordDialogListener = passwordDialogListener;
            return this;
        }

        /**
         * 单按钮对话框和双按钮对话框的公共部分在这里设置
         */
        public PasswrdInputDialog create() {
            TextView dialog_close=layout.findViewById(R.id.dialog_close);
            dialog_close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            final VerificationCodeView pwdEditText=layout.findViewById(R.id.com_dialog_pwd_ev);
            pwdEditText.setVisibility(View.VISIBLE);
            pwdEditText.setFocusable(true);
            pwdEditText.setFocusableInTouchMode(true);
            pwdEditText.requestFocus();
            pwdEditText.setInputCompleteListener(new VerificationCodeView.InputCompleteListener(){

                @Override
                public void inputComplete() {
                    if(pwdEditText.getInputContent()!=null&&pwdEditText.getInputContent().length()==6){
                        dialog.dismiss();
                        passwordDialogListener.onBtn2Click(pwdEditText.getInputContent());
                    }
                }

                @Override
                public void deleteContent() {

                }
            });

            TextView messageTv=layout.findViewById(R.id.com_dialog_message_tv);
            TextView titleTv=layout.findViewById(R.id.com_dialog_title_tv);
            messageTv.setVisibility(View.GONE);
            titleTv.setVisibility(View.GONE);
            if (message != null) {      //设置提示内容
                messageTv.setText(Html.fromHtml(message));
                messageTv.setVisibility(View.VISIBLE);
            }
            if (title != null) {
                titleTv.setText(Html.fromHtml(title));
                titleTv.setVisibility(View.VISIBLE);
            }
            Button btn1=layout.findViewById(R.id.com_dialog_b1_bt);
            Button btn2=layout.findViewById(R.id.com_dialog_b2_bt);
            btn1.setVisibility(View.GONE);
            btn2.setVisibility(View.GONE);
            if(btn1Text!=null){
                btn1.setVisibility(View.VISIBLE);
                btn1.setText(btn1Text);
            }
            if(btn2Text!=null){
                btn2.setVisibility(View.VISIBLE);
                btn2.setText(btn2Text);
            }
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    if (passwordDialogListener != null) {
                        passwordDialogListener.onBtn1Click(pwdEditText.getInputContent());
                    }
                }
            });
            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    if (passwordDialogListener != null) {
                        passwordDialogListener.onBtn2Click(pwdEditText.getInputContent());
                    }
                }
            });
            dialog.setContentView(layout);
            dialog.setCancelable(false);     //用户可以点击手机Back键取消对话框显示
            dialog.setCanceledOnTouchOutside(false);        //用户不能通过点击对话框之外的地方取消对话框显示
            return dialog;
        }
    }

    public static interface PasswordDialogListener {
        void onBtn1Click(String password);
        void onBtn2Click(String password);
    }
}
